package cn.hospital.seviceimpl;

import cn.hospital.dao.*;
import cn.hospital.entity.*;
import cn.hospital.service.GroupingService;
import cn.hospital.util.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class GroupingServiceImpl implements GroupingService {

    @Resource
    private HospitalMapper hospitalMapper;
    @Resource
    private DepartmentMapper departmentMapper;
    @Resource
    private RankMapper rankMapper;
    @Resource
    private GrouptableMapper grouptableMapper;
    @Resource
    private MeaningUserMapper meaningUserMapper;

    @Override
    public List<Hospital> hospitalfin() {

        return hospitalMapper.findHospital();
    }

    @Override
    public List<Department> depeartmentfin(String hospitalId) {
        return departmentMapper.findByhospitalId(hospitalId);
    }

    @Override
    public Department depeartmentId(Integer depeartmentId) {
        return departmentMapper.depeartmentId(depeartmentId);
    }

    @Override
    public List<MeaningUser> selectMeaningUser(Department department) {
        return meaningUserMapper.selectMeaningUser(department);
    }

    @Override
    public List<Rank> rankfin(Integer rankType) {
        return rankMapper.selectRankList(rankType);
    }

    @Override
    public int groupingAdd(Grouptable grouptable) throws ParseException {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        grouptable.setBeginTime(simpleDateFormat.parse(grouptable.getBeginTimes()));
        grouptable.setCreatorTime(simpleDateFormat.parse(grouptable.getCreatorTimes()));
        grouptable.setEndTime(simpleDateFormat.parse(grouptable.getEndTimes()));
        return grouptableMapper.insertGrouptable(grouptable);
    }

    @Override
    public PageInfo<Grouptable> groupingList(Grouptable grouptable, Integer currentPage) throws ParseException {
        if(grouptable!=null){
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
            if(grouptable.getEndTimes()!=null && grouptable.getEndTimes()!=""){
                grouptable.setEndTime(simpleDateFormat.parse(grouptable.getEndTimes()));
            }
            if(grouptable.getBeginTimes()!=null && grouptable.getBeginTimes()!=""){
                grouptable.setBeginTime(simpleDateFormat.parse(grouptable.getBeginTimes()));
            }
            if(grouptable.getCreatorTimes()!=null && grouptable.getCreatorTimes()!=""){
                grouptable.setCreatorTime(simpleDateFormat.parse(grouptable.getCreatorTimes()));
            }
        }
        Integer totalNum=grouptableMapper.selectGrouptableCount(grouptable);
        if(currentPage==null){
            currentPage=1;
        }
        PageInfo<Grouptable> pageInfo = new PageInfo<>(currentPage,5,totalNum);
        List<Grouptable> grouptableList = grouptableMapper.selectGrouptableList(grouptable,pageInfo.getStartNum(),pageInfo.getPageSize());
        pageInfo.setList(grouptableList);
        return pageInfo;
    }

    @Override
    public int groupDelete(Integer[] groupId) {
        Integer delete=0;
        for(Integer groupid:groupId){
            delete=grouptableMapper.groupDelete(groupid);
            if(delete==0){
                break;
            }
        }
        return delete;
    }

    @Override
    public int groupUpdate(Grouptable grouptable) throws ParseException {
        if(grouptable!=null){
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
            if(grouptable.getEndTimes()!=null && grouptable.getEndTimes()!=""){
                grouptable.setEndTime(simpleDateFormat.parse(grouptable.getEndTimes()));
            }
            if(grouptable.getBeginTimes()!=null && grouptable.getBeginTimes()!=""){
                grouptable.setBeginTime(simpleDateFormat.parse(grouptable.getBeginTimes()));
            }
            if(grouptable.getCreatorTimes()!=null && grouptable.getCreatorTimes()!=""){
                grouptable.setCreatorTime(simpleDateFormat.parse(grouptable.getCreatorTimes()));
            }
        }
        return grouptableMapper.groupUpdate(grouptable);
    }

    @Override
    public Grouptable selectGroupOne(Integer groupId) {
        Grouptable grouptable=grouptableMapper.selectGroupOne(groupId);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        if(grouptable.getBeginTime()!=null){
            grouptable.setBeginTimes(simpleDateFormat.format(grouptable.getBeginTime()));
        }
        if(grouptable.getEndTime()!=null){
            grouptable.setEndTimes(simpleDateFormat.format(grouptable.getEndTime()));
        }
        return grouptable;
    }

    /**
     * 查询随访计划
     *
     * @return
     */
    @Override
    public List<Grouptable> findGrouptablr() {
        return grouptableMapper.selectGrouptablr();
    }
}
